package com.enjoy.prepare.proxy;


import com.enjoy.prepare.proxy.jdkdynamic.JamesInvocationHandler;
import sun.misc.ProxyGenerator;

import java.io.*;
import java.lang.reflect.Proxy;

/**
 * 动态代理测试
 */
public class LisonDyna {
    public static void main(String[] args) {
        // 代理类
        IGetServant james = (IGetServant)
                Proxy.newProxyInstance(IGetServant.class.getClassLoader(),
                        new Class[]{IGetServant.class},
                        new JamesInvocationHandler(new Receptionist()));
        // 把代理类输出到一个文件中
        createProxyClass();
        james.choice("御姐范风格");
    }

    public static void createProxyClass() {
        final byte[] bytes = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{IGetServant.class});
        try {
            final FileOutputStream fileOutputStream = new FileOutputStream("$Proxy0.class");
            fileOutputStream.write(bytes);
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
